package git.soulbgm.shiro.authentication;

import git.soulbgm.pojo.User;
import git.soulbgm.shiro.JdbcUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @author SoulBGM
 * @version 1.0
 * @date 2019/9/25 14:27
 */
public class JdbcRealm3 implements Realm {
    @Override
    public String getName() {
        return "jdbcRealm3";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        String password = new String((char[]) token.getCredentials());
        User user = JdbcUtil.findOne("select * from user where mailbox = ? and password = ?", User.class, username, password);
        if (user != null) {
            return new SimpleAuthenticationInfo(user, password, getName());
        } else {
//            throw new IncorrectCredentialsException();
            return null;
        }
    }
}
